package cn.e3.search.service.impl;

import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import search.SearchResult;
import cn.e3.search.service.SearchResultService;
@Service
public class SearchResultServiceImpl implements SearchResultService{
	@Autowired
	private SearchResultDao searchResultDao;
	//商品搜索服务层
	public SearchResult search(String keyword, int page, int rows) throws Exception {
		//创建查询对象
		SolrQuery solrQuery=new SolrQuery();
		//设置查询条件
		solrQuery.setQuery(keyword);
		//设置分页条件
		if(page<0) page=1;
		//开始查询索引
		solrQuery.setStart((page-1)*rows);
		//每页显示条数
		solrQuery.setRows(rows);
		//设置默认搜索与
		
		solrQuery.set("df","item_title");
		//开启高亮
		solrQuery.setHighlight(true);
		//设置高亮显示域
		solrQuery.addHighlightField("item_title");
		//设置高亮显示内容
		solrQuery.setHighlightSimplePre("<em style=\"color:red\">");
		solrQuery.setHighlightSimplePost("</em>");
		//调用ddao执行查询
		SearchResult searchResult = searchResultDao.search(solrQuery);
		//总条数除以总页数
		long count = searchResult.getRecordCount();
		int totalPage=(int) (count/rows);
		 if(count%rows>0){
			 totalPage++;
		 }
		 searchResult.setTotalPages(totalPage);
		return searchResult;
	}

}
